package br.com.CF.activitys;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.ProgressBar;
import br.com.CF.R;
import br.com.CF.dao.ContaDao;
import br.com.CF.entidades.Conta;

public class MainActivity extends Activity {

	private ProgressBar progress;
	private ContaDao dao;
	private Boolean exec = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.main_layout);

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				int i = 0;
				List<Conta> c = new ArrayList<Conta>();

				while(exec == true){
					Log.i("ControleFinanceiro", "Executando Thread...");
					progress = (ProgressBar)findViewById(R.id.progress);

					if(i == 1){
						progress.setProgress(4);

						dao = new ContaDao(MainActivity.this);
						progress.setProgress(5);

						c = dao.getAll();
						progress.setProgress(6);

					}else if(i == 2){
						Log.i("ControleFinanceiro", "Quantidade de registros: "+c.size());
						if(c.size() <= 0){
							Intent intent = new Intent(MainActivity.this, AddContaActivity.class);
							progress.setProgress(8);

							exec = false;
							finish();

							startActivity(intent);
						}else{
							Intent intent = new Intent(MainActivity.this, InicialActivity.class);
							progress.setProgress(8);

							exec = false;
							finish();

							startActivity(intent);
						}	
					}
					i++;
					try {
						Thread.sleep(2500);
					} catch(InterruptedException e) {
						e.printStackTrace();
					}
					Log.i("ControleFinanceiro", "Thread executada 1...");
				}
			}				
		};
		new Thread(runnable).start();
	}
}
